package com.book.remote;

import com.book.activeMQ.productor.ProductMQ;
import com.book.common.BaseController;
import com.book.common.RedisUserCacheEnum;
import com.book.dto.UserDto;
import com.book.helper.UserHelper;
import com.book.pojo.User;
import com.book.service.ShopCarService;
import com.book.service.UserService;
import com.book.util.Constants;
import com.book.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author 李锦程
 * @Date 2021/4/4 5:35 下午
 * @Package com.book.service.impl
 * @Version 1.0
 */
@Component
public class UserRemote extends BaseController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private ShopCarService shopCarService;
    @Autowired
    private ProductMQ productMQ;


    public Boolean userPhoneCheck(UserDto user) {
        if (redisUtil.sIsMember(Constants.USER_PHONE, user.getUPhone())) {
            return false;
        } else {
            User userByPhone = userService.getUserByPhone(user);
            if (userByPhone != null) {
                redisUtil.sAdd(Constants.USER_PHONE, user.getUPhone());
                return false;
            } else {
                return true;
            }
        }
    }

    public Boolean userNameCheck(UserDto userDto) {
        if (redisUtil.sIsMember(Constants.USER_NAME, userDto.getUName())) {
            return false;
        } else {
            User user = userService.getUserByName(userDto);
            if (user != null) {
                redisUtil.sAdd(Constants.USER_NAME, user.getUName());
                return false;
            } else {
                return true;
            }
        }
    }

    public Boolean userEmailCheck(UserDto userDto) {
        if (redisUtil.sIsMember(Constants.USER_EMAIL, userDto.getUEmail())) {
            return false;
        } else {
            User user = userService.getUserByEmail(userDto);
            if (user != null) {
                redisUtil.sAdd(Constants.USER_EMAIL, user.getUEmail());
                return false;
            } else {
                return true;
            }
        }
    }

    public Integer register(UserDto userDto) {

        return userService.registerUser(userDto);
    }

    public UserDto doLogin(UserDto userDto) {
        return userService.doLogin(userDto);
    }

    public Integer getCountByName(String name) {
        return userService.getCountByName(name);
    }

    public Integer getCountByPhone(String phone) {
        return userService.getCountByPhone(phone);
    }

    public Integer getCountByEmail(String email) {
        return userService.getCountByEmail(email);
    }

    public Boolean updateUser(String key,String newValue,String oldValue,UserDto userDto) {
        Integer integer = userService.updateUser(UserHelper.convertDtoToPo(userDto));
        swapRedisSet(key,newValue,oldValue);
        return integer == 1;
    }

    private void swapRedisSet(String key,String newValue,String oldValue){
        if (!redisUtil.sIsMember(key,oldValue)){
            redisUtil.sAdd(key,newValue);
            return;
        }
        Boolean aBoolean = redisUtil.swapSetValue(key, newValue, oldValue);
        if (aBoolean){
            return;
        }
        redisUtil.delRedisKey(key);
        productMQ.updaeRedisUserCache(RedisUserCacheEnum.getId(key));
    }

    public Boolean updatePassword(String pass, String userCode) {
        return userService.updatePassword(pass,userCode);
    }

    public Boolean validAccount(String account,String uCode) {
        User userByCode = userService.getUserByCode(uCode);
        if (userByCode == null){
            return false;
        }
        return account.equals(userByCode.getUEmail()) || account.equals(userByCode.getUPhone());
    }
}
